package com.wutongyu.mannyburger;

import java.io.Serializable;

/**
 * 购物车项数据模型，包含商品和数量信息
 */
public class CartItem implements Serializable {
    private Product product;
    private int quantity;

    public CartItem() {
        this.quantity = 0;
    }

    public CartItem(Product product, int quantity) {
        this.product = product;
        this.quantity = quantity;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public void increaseQuantity() {
        this.quantity++;
    }

    public void decreaseQuantity() {
        if (this.quantity > 0) {
            this.quantity--;
        }
    }

    /**
     * 获取当前项的总价
     * @return 数量*单价
     */
    public double getTotalPrice() {
        return product != null ? product.getPrice() * quantity : 0;
    }

    /**
     * 商品是否在购物车中（数量大于0）
     */
    public boolean isInCart() {
        return quantity > 0;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CartItem cartItem = (CartItem) o;
        return product != null && cartItem.product != null && 
               product.getId() == cartItem.product.getId();
    }

    @Override
    public int hashCode() {
        return product != null ? product.getId() : 0;
    }

    @Override
    public String toString() {
        return "CartItem{" +
                "product=" + (product != null ? product.getName() : "null") +
                ", quantity=" + quantity +
                ", totalPrice=" + getTotalPrice() +
                '}';
    }
} 